package com.pulanit.pangu.admin.web.config;

import cn.hutool.core.collection.ListUtil;
import com.pulanit.pangu.admin.common.config.PanguConfig;
import com.pulanit.pangu.admin.common.utils.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.CacheControl;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Configuration
public class AdminWebMvcConfigurer implements WebMvcConfigurer {

    @Autowired
    private JwtInterceptor jwtInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        regJwt(registry);
    }

    private void regJwt(InterceptorRegistry registry){
        List<String> excludePathPatterns = ListUtil.list(false);
        excludePathPatterns.add("/**/login");
        excludePathPatterns.add("/**/register");
        excludePathPatterns.add("/**/validateUserName");
        excludePathPatterns.add("/profile/**");
        excludePathPatterns.add("/open/**");
        registry.addInterceptor(jwtInterceptor).addPathPatterns("/**").excludePathPatterns(excludePathPatterns);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry)
    {
        /** 本地文件上传路径 */
        registry.addResourceHandler(Constants.RESOURCE_PREFIX + "/**")
                .addResourceLocations("file:" + PanguConfig.getProfile() + "/");

    }

}
